<?php
/**
 * Created by PhpStorm.
 * User: LG
 * Date: 2017/9/21
 * Time: 17:16
 */

namespace app\common\model;

/*
 *  行政区域模型
 */
use think\Db;
use think\Model;

class Administrative extends Model
{
    protected $db;
    public function __construct()
    {
        parent::__construct();
        $this->db = Db::name('administrative_info');
    }

    /*
	 *  统计行政区域数量
	 *  @author ligang
	 *  @param  $where      条件
	 *  @param  $whereTime  时间条件['字段'=>'规则']
	 *  @return int
	 */
    public function CountAdministrativeNumber($where,$whereTime,$join=[])
    {
        if (empty($join)){
            if (empty($whereTime)){
                $result = $this->db->where($where)->count();
            }else{
                if (!is_array($whereTime)){
                    return "时间必须是数组,以键值对形式";
                }
                $whereTimeKey = array_keys($whereTime);
                $result = $this->db->where($where)->whereTime($whereTimeKey[0],$whereTime[$whereTimeKey[0]])->count();
            }
        }else{
            if (empty($whereTime)){
                $result = $this->db->where($where)->join($join)->count();
            }else{
                if (!is_array($whereTime)){
                    return "时间必须是数组,以键值对形式";
                }
                $whereTimeKey = array_keys($whereTime);
                $result = $this->db->join($join)->where($where)->whereTime($whereTimeKey[0],$whereTime[$whereTimeKey[0]])->count();
            }
        }
        return $result;
    }

    /*
     * 查询一条行政区域账号信息
     * @author  ligang
     * @param   array   $where    条件
     * @param   array   $join     关联条件
     * @param   string  $field    查询字段
     * @return  array
     */
    public function findUserInfo($where = [],$join = [],$field = '*')
    {
        if (empty($join)){
            $result = Db::name('administrative_user')->where($where)->field($field)->find();
        }else{
            $result = Db::name('administrative_user')->where($where)->join($join)->field($field)->find();
        }
        return $result;
    }
    /**
     * 查询一条行政中心信息
     */
    public function findOne($where, $field = '*')
    {
    	
    	$result = Db::name('administrative_info')->where($where)->field($field)->find();
    	// 判断结果 并返回
    	if ($result == false) {
    		return false;
    	}
    	return $result;
    }
     /**
     * 查询
     */
    public function getAll($where, $field = '*',$group='')
    {
        if(!empty($group)){
            $result = Db::name('administrative_info')->where($where)->field($field)->group($group)->select();
        }else{
            $result = Db::name('administrative_info')->where($where)->field($field)->select();
        }
        
        // 判断结果 并返回
        if ($result == false) {
            return false;
        }
        return $result;
    }

    /**
     * @param $where
     * @return array|bool|false|\PDOStatement|string|Model
     *       查询一条行政区域用户表数据
     */
    public function getAdministrativeUser($where)
    {
        $result = Db::name('administrative_user')->where($where)->find();
        // 判断结果 并返回
        if ($result == false) {
            return false;
        }
        return $result;
    }

    /**
     * @param $where
     * @return array|bool|false|\PDOStatement|string|Model
     *         查询登录用户所属哪个角色
     */
    public function getAdministrativeGroup($where)
    {
        $result = Db::name('administrative_auth_group')->where($where)->find();
        // 判断结果 并返回
        if ($result == false) {
            return false;
        }
        return $result;
    }
    
    // 查询数量
    public function getCount($where=[])
    {
        $result = $this->db->where($where)->count();
        return $result;
    }

}